<?php namespace Phpcmf\Controllers\Member;

class Edit extends \Phpcmf\Member\Module
{

	// 内容修改
	public function edit() {
		if (IS_POST) {
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			$data_edit = \Phpcmf\Service::L('input')->post('data');
			$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->get()->getRowArray();
			// 判断 $shuju_data 是否为空
			if(!$shuju_data){
				$data = [
					'fujian' => '',
					'neirong' => '',
				];
				$this->_json('0', '数据不存在', $data);
			}
			/*if($shuju_data['status']==0){
				$data = [
					'fujian' => '',
					'neirong' => '',
				];
				$this->_json('0', '帖子核对中...', $data);
			}*/

			if($this->member['is_admin'] || $this->uid == $shuju_data['uid']){
				// 循环图片
				$tupian_list = [];
				$tupian_data_list = dr_string2array($data_edit['tupian']['id']);
				foreach ($tupian_data_list as $key => $value) {
					$tupian_list['file'][$key] = $value;
					$tupian_list['title'][$key] = '';
					$tupian_list['description'][$key] = '';
				}
				$tupian_data = json_encode($tupian_list, JSON_UNESCAPED_UNICODE);
				// 循环音频
				$yinpin_list = [];
				$yinpin_data_list = dr_string2array($data_edit['yinpin']['id']);
				foreach ($yinpin_data_list as $key => $value) {
					$yinpin_list['file'][$key] = $value;
					$yinpin_list['title'][$key] = '';
					$yinpin_list['description'][$key] = '';
				}
				$yinpin_data = json_encode($yinpin_list, JSON_UNESCAPED_UNICODE);
				//{"file":["133","137"],"title":["",""],"description":["",""]}
				//$tupian_data = dr_string2array($tupian_list);
				$neirong = [
					'title' => $data_edit['title'],
					'tupian' => $tupian_data,
					'yinpin' => $yinpin_data,
					'neirong' => $data_edit['neirong'], //内容
					'inputip' => \Phpcmf\Service::L('input')->ip_info(),//修改者ip
					'uptime' => time(),
					'status' => 0,
					'gongkai' => $data_edit['gongkai'], //公开
					'moshi' => $data_edit['moshi'], //模式
				];
				// 修改帖子内容
				\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->update($neirong);

				$data = [
					'fujian' => '',
					'neirong' => $neirong,
				];
				$this->_json('1', '修改成功', $data);

			} else {
				$data = [
					'fujian' => '',
					'neirong' => '',
				];
				$this->_json('0', '无权操作', $data);
			}
			

		} else {
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->get()->getRowArray();
			// 循环 附件
			$tupian_list = [];
			$tu_data = dr_string2array($shuju_data['tupian']);
			foreach ($tu_data['file'] as $key => $value) {
				$tupian_data = \Phpcmf\Service::M()->db->table('attachment_data')->where('id', $value)->get()->getRowArray();
				$tupian_list[$key] = [
					'id' => $value,
					'url' => SITE_URL.'uploadfile/'.$tupian_data['attachment'],
					'size' => $tupian_data['filesize'],
					'type' => 'image/'.$tupian_data['fileext'],
					'name' => $tupian_data['name'],
					'status' => 'success',
				];
			}
			// 循环 音频
			$yinpin_list = [];
			$yinpin_data = dr_string2array($shuju_data['yinpin']);
			foreach ($yinpin_data['file'] as $key => $value) {
				$yinpin_data = \Phpcmf\Service::M()->db->table('attachment_data')->where('id', $value)->get()->getRowArray();
				$yinpin_list[$key] = [
					'id' => $value,
					'url' => SITE_URL.'uploadfile/'.$yinpin_data['attachment'],
					'size' => $yinpin_data['filesize'],
					'type' => 'audio/'.$yinpin_data['fileext'],
					'name' => $yinpin_data['name'],
					'status' => 'success',
				];
			}

			// 判断 $shuju_data 是否为空
			if(!$shuju_data){
				$data = [
					'fujian' => '',
					'neirong' => '',
				];
				$this->_json('0', '数据不存在', $data);
			}

			if($this->member['is_admin'] || $this->uid == $shuju_data['uid']){

                $neirong = [
                    'tupian' => $tupian_list, //图片
					'yinpin' => $yinpin_list, //音频
					'neirong' => htmlspecialchars_decode($shuju_data['neirong']), // 内容
					
				];
				$data = [
					'fujian' => '',
					'neirong' => $neirong,
					'data' => $shuju_data,
				];
				$this->_json('1', '读取成功', $data);

			} else {
				$data = [
					'fujian' => '',
					'neirong' => '',
				];
				$this->_json('0', '无权查看', $data);
			}
		}
	}
	//隐藏
	public function yincang() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$gongkai = (int)\Phpcmf\Service::L('input')->get('gongkai');
		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->where('status', 1)->get()->getRowArray();
		// 判断 $shuju_data['gongkai'] 是否等于 1
		if($shuju_data['gongkai'] == 1){
			$gongkai_zhi = 0;
			$msg = '隐藏成功';
		}else{
			$gongkai_zhi = 1;
			$msg = '公开成功';
		}
		if($this->member['is_admin'] || $this->uid == $shuju_data['uid']){
			//修改gongkai参数
			$up_data = [
				'gongkai' => $gongkai_zhi,
			];
			\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->update($up_data);
			$data = [
				'gongkai' => $gongkai_zhi,
			];

			$this->_json('1', $msg ,$data);

		} else {
			$data = [
				'gongkai' => $gongkai_zhi,
			];
			$this->_json('0', '没有权限',$data);

		}
		

		
	}
    // 删除
	public function del() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->where('status', 1)->get()->getRowArray();
		if($this->member['is_admin'] || $this->uid == $shuju_data['uid']){
			\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->delete();
			\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng_data_0')->where('id', $id)->delete();
			$this->_json('1', '删除成功');
		}else{
			$this->_json('0', '没有权限');
		}
	}
	//修改知识
	public function edit_quan() {
		if (IS_POST) {
			$id = (int)\Phpcmf\Service::L('input')->get('id');
			$title = \Phpcmf\Service::L('input')->post('title');
			$thumb = \Phpcmf\Service::L('input')->post('thumb');
			$kaifangfabu = \Phpcmf\Service::L('input')->post('kaifangfabu');
			$neirong = \Phpcmf\Service::L('input')->post('neirong');
			$description = \Phpcmf\Service::L('input')->post('description');

			$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi')->where('id', $id)->get()->getRowArray();
			if(!$thumb){
				$thumb = $shuju_data['thumb'];
			}
			if($this->member['is_admin'] || $this->uid == $shuju_data['uid']){
				$neirong = [
					'title' => $title,
					'thumb' => $thumb,
					'kaifangfabu' => $kaifangfabu,
					'description' => $description,
					//'neirong' => $data_edit['neirong'], //内容
					'inputip' => \Phpcmf\Service::L('input')->ip_info(),//修改者ip
					'updatetime' => time(),
					//'status' => 0,
				];
				// 修改帖子内容
				\Phpcmf\Service::M()->db->table('1_zhishi')->where('id', $id)->update($neirong);
				$neirong_0 = [
					'content' => $description,
				];
				\Phpcmf\Service::M()->db->table('1_zhishi_data_0')->where('id', $id)->update($neirong_0);

				$this->_json('1', '修改成功', $neirong);
			}else{
				$this->_json('0', '没有权限');
			}
		} else {
			$this->_json('0', '参数错误');
		}
	}
}
